﻿using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Markup;
using System.Diagnostics;

namespace Vision.Core.ActivityComponents.Diagnostics
{
    /// <summary>
    /// 
    /// </summary>
    [Designer(typeof(EventLogDesigner))]
    public class EventLogWrite : NativeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        [DefaultValue(null)]
        public InArgument<string> LogName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        [RequiredArgument]
        [DefaultValue(null)]
        public InArgument<string> Source { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        [RequiredArgument]
        [DefaultValue(null)]
        public InArgument<string> Message { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        public EventLogEntryType LogEntryType { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="metadata"></param>
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            // add the arguments to the argument collection
            metadata.AddArgument(new RuntimeArgument("Source", typeof(string), ArgumentDirection.In, true));
            metadata.AddArgument(new RuntimeArgument("Message", typeof(string), ArgumentDirection.In, true));
            metadata.AddArgument(new RuntimeArgument("LogEntryType", typeof(EventLogEntryType), ArgumentDirection.In, false));

            metadata.AddArgument(new RuntimeArgument("LogName", typeof(string), ArgumentDirection.In, false));
            
            //EventLogEntryType.FailureAudit;
            //EventLogEntryType.Information;
            //EventLogEntryType.SuccessAudit;
            //EventLogEntryType.Warning;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            if (this.LogName != null && this.LogName.Get(context) != null)
            {
                if (!EventLog.SourceExists(this.Source.Get(context)))
                    EventLog.CreateEventSource(this.Source.Get(context), this.LogName.Get(context));
            }

            EventLog.WriteEntry(this.Source.Get(context), this.Message.Get(context), this.LogEntryType);
        }
    }    
}
